<?php
/*
Page:           _drawrating.php
Created:        Aug 2006
The function that draws the rating bar.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/
--------------------------------------------------------- */
function rating_bar($id,$units='') { 

require('_config-rating.php'); // get the db connection info
	
//set some variables
if (getenv('HTTP_CLIENT_IP')) {
	$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
	$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
	$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
	$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
	$ip = getenv('HTTP_FORWARDED');
}
else {
	$ip = $_SERVER['REMOTE_ADDR'];
}
if (!$units) {$units = 10;}

$query=$db->uzklausa("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ") or die(" Error: ".mysql_error());
if (mysql_affected_rows() == 0)	{ $db->uzklausa("INSERT INTO $tableName VALUES ('$id', 0, 0, NULL)") or die("<script>alert('".mysql_error()."')</script>"); }
$numbers=mysql_fetch_assoc($query);
$count=$numbers['total_votes']; //how many votes total
$current_rating=$numbers['total_value']; //total number of rating added together and stored
$tense=($count==1) ? "balsas" : "balsai(-u)"; //plural form votes/vote

// determine whether the user has voted, so we know how to draw the ul/li
$voted=mysql_num_rows($db->uzklausa("SELECT used_ips FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' ")); 

// now draw the rating bar
?>
<div class="ratingblock">

<div id="unit_long<?php echo $id ?>">
		<ul id="unit_ul<?php echo $id ?>" class="unit-rating" style="width:<?php echo $unitwidth*$units; ?>px;">
		<li class="current-rating" style="width:<?php echo @number_format($current_rating/$count,2)*$unitwidth; ?>px;"title="<?php echo $id ?>. Balai: <?php echo @number_format($current_rating/$count,1) ?>/<?php echo $units ?> (<?php echo $count." ".$tense ?>)">Currently <?php echo @number_format($current_rating/$count,2); ?>/<?php echo $units ?></li>
<?php
	for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
		if(!$voted) { // if the user hasn't yet voted, draw the voting stars 
		?>
<li><a href="rate/db.php?j=<?php echo $ncount ?>&q=<?php echo $id ?>&t=<?php echo $ip ?>&c=<?php echo $units ?>" title="<?php echo $id ?>. Balai: <?php echo @number_format($current_rating/$count,1) ?>/<?php echo $units ?> (<?php echo $count." ".$tense ?>)" class="r<?php echo $ncount ?>-unit rater"><?php echo $ncount ?></a></li>
<?php
	 } 
  }
	$ncount=0; // resets the count
?>
		</ul>
</div>
</div>
<?
}
?>